﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.K3.Core.MFG.EntityHelper;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Caraya.MFG.BusinessPlugIn
{
    /// <summary>
    /// 模拟报价单子项明细上移下移
    /// </summary>
    [Description("模拟报价项次修改"), HotUpdate]
    public class SimulateQuotationReplaceGroupEdit : AbstractBillPlugIn
    {

        public override void EntryBarItemClick(BarItemClickEventArgs e)
        {
            base.EntryBarItemClick(e);
            switch (e.BarItemKey)
            {
                case "tbMoveUp":
                    ExchangeEntryRow(isMoveUp: true);
                    break;
                case "tbMoveDown":
                    ExchangeEntryRow(isMoveUp: false);
                    break;
                default:
                    break;
            }
        }
        private void ExchangeEntryRow(bool isMoveUp)
        {
            EntryEntity entryEntity = base.View.BusinessInfo.GetEntryEntity("FDetailEntity");
            DynamicObjectCollection entityDataObject = base.View.Model.GetEntityDataObject(entryEntity);
            Dictionary<int, IGrouping<int, DynamicObject>> dictionary = (from r in entityDataObject
                                                                         where r.GetDynamicObjectItemValue("Seq", 0) > 0
                                                                         select r into e
                                                                         group e by e.GetDynamicObjectItemValue("Seq", 0)).ToDictionary((IGrouping<int, DynamicObject> g) => g.Key);
            if (dictionary.IsEmpty())
            {
                return;
            }
            int dynamicObjectItemValue = base.View.GetSelectedRowDatas(entryEntity.Key).FirstOrDefault().GetDynamicObjectItemValue("Seq", 0);
            int num = (isMoveUp ? (dynamicObjectItemValue - 1) : (dynamicObjectItemValue + 1));
            if (num < 1 || num > dictionary.Count)
            {
                return;
            }
            dictionary.TryGetValue(dynamicObjectItemValue, out var value);
            dictionary.TryGetValue(num, out var value2);
            if (value.IsEmpty() || value2.IsEmpty())
            {
                return;
            }
            foreach (DynamicObject item in value)
            {
                base.View.Model.DeleteEntryRow("FDetailEntity", entityDataObject.IndexOf(item));
            }
            DynamicObject dynamicObject = value.FirstOrDefault();
            //string dynamicValue = dynamicObject.GetDynamicValue<string>("ISSUETYPE");
            //string dynamicValue2 = dynamicObject.GetDynamicValue<string>("BACKFLUSHTYPE");
            int num2 = 0;
            int num3 = value2.Count();
            for (int i = 0; i < num3; i++)
            {
                DynamicObject dynamicObject2 = value2.ElementAt(i);
                dynamicObject2.SetDynamicObjectItemValue("Seq", dynamicObjectItemValue);
                int num4 = entityDataObject.IndexOf(dynamicObject2);
                if (i == 0)
                {
                    num2 = num4;
                }
                else if (isMoveUp)
                {
                    if (num4 < num2)
                    {
                        num2 = num4;
                    }
                }
                else if (num4 > num2)
                {
                    num2 = num4;
                }
            }
            num2 = (isMoveUp ? num2 : (num2 + 1));
            DynamicObject dynamicObject3 = value.FirstOrDefault((DynamicObject w) => string.IsNullOrWhiteSpace(w.GetDynamicObjectItemValue<string>("ParentRowId")) && w.GetDynamicObjectItemValue("IskeyItem", defValue: false));
            foreach (DynamicObject item2 in value)
            {
                item2.SetDynamicObjectItemValue("Seq", num);
                if (dynamicObject3 != item2)
                {
                    base.View.Model.CreateNewEntryRow(entryEntity, num2, item2);
                }
            }
            if (dynamicObject3 != null)
            {
                base.View.Model.CreateNewEntryRow(entryEntity, num2, dynamicObject3);
            }
            //base.View.Model.SetValue("FISSUETYPE", dynamicValue, num2);
            //base.View.Model.SetValue("FBACKFLUSHTYPE", dynamicValue2, num2);

            // 只保存数据，不更新界面用BusinessDataServiceHelper.Save
            // var OperationResult = BusinessDataServiceHelper.Save(this.Context, View.BusinessInfo, View.Model.DataObject);
            base.View.UpdateView("FDetailEntity");
            base.View.SetEntityFocusRow("FDetailEntity", num2);
            TreeEntryGrid control = base.View.GetControl<TreeEntryGrid>("FDetailEntity");
            control.CollapsedRow(num2);
        }

    }
}
